//
//  SharePublicServerView.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/6.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "SharePublicServerView.h"
#import "PublicServerCell.h"

@interface SharePublicServerView ()<UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UICollectionView *collectView;
//数据源
@property (nonatomic, strong) NSArray *collectDataArray;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *cancleBtn;



@end


@implementation SharePublicServerView


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return nil;
}

- (instancetype) initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self setUpData];
        [self createView];
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    }

    return self;
}


- (void) setUpData {
    NSDictionary *dic1 = @{@"image":@"分享微信好友",
                           @"title":NSLocalString(@"微信好友")};
    NSDictionary *dic2 = @{@"image":@"分享朋友圈",
                           @"title":NSLocalString(@"朋友圈")};
    NSDictionary *dic3 = @{@"image":@"分享微博",
                           @"title":NSLocalString(@"微博")};
    NSDictionary *dic4 = @{@"image":@"分享QQ",
                           @"title":NSLocalString(@"QQ")};
    NSDictionary *dic5 = @{@"image":@"举报",
                           @"title":NSLocalString(@"举报")};
    
    self.collectDataArray = @[dic1, dic2, dic3, dic4, dic5];

}

- (void) createView {

    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-500*m6Scale, KScreenWidth, 500*m6Scale)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    self.bottomView.userInteractionEnabled = YES;
    [self addSubview:self.bottomView];
    

    //self.cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 402 * m6Scale, KScreenWidth, 98 * m6Scale)];
    //self.cancleBtn.backgroundColor = [UIColor clearColor];
    //[self.cancleBtn setTitle:NSLocalString(@"取消") forState:0];
    //[self.cancleBtn setTitleColor:RGB(103, 103, 103) forState:0];
    //[self.cancleBtn addTarget:self action:@selector(clickCancleBtn) forControlEvents:UIControlEventTouchUpInside];
    //[self.bottomView addSubview:self.cancleBtn];
    
    [self initCollectionView];
}

- (void)initCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10 * m6Scale;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 500 * m6Scale) collectionViewLayout:layout];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.showsVerticalScrollIndicator = NO;
    self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView.backgroundColor = [UIColor whiteColor];
    
    
    //注册 colllectionCell
    [self.collectView registerClass:[PublicServerCell class] forCellWithReuseIdentifier:@"PublicServerCell"];
    [self.collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
    [self.bottomView addSubview:self.collectView];
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 200 * m6Scale, KScreenWidth, 2 * m6Scale)];
    topLine.backgroundColor = BackgroundColor;
    [self.collectView addSubview:topLine];
    [self.bottomView bringSubviewToFront:topLine];
    
    //UIView *midLine = [[UIView alloc] initWithFrame:CGRectMake(0, 398 * m6Scale, KScreenWidth, 2 * m6Scale)];
    //midLine.backgroundColor = BackgroundColor;
    //[self.collectView addSubview:midLine];
    //[self.bottomView bringSubviewToFront:midLine];

}

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectDataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PublicServerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PublicServerCell" forIndexPath:indexPath];
    [cell.signImg mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(cell.mas_centerX);
        make.top.mas_equalTo(cell.mas_top).offset(20 * m6Scale);
        make.size.mas_equalTo(CGSizeMake(100 * m6Scale, 100 * m6Scale));
    }];
    
    NSDictionary *dicData = self.collectDataArray[indexPath.row];
    cell.titleLab.text = dicData[@"title"];
    cell.signImg.image = [UIImage imageNamed:dicData[@"image"]];
   
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(KScreenWidth, 106*m6Scale);
}

// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        return nil;
    }
    reusableView.backgroundColor = [UIColor greenColor];
    if (kind == UICollectionElementKindSectionFooter)
    {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        footerview.backgroundColor = BackgroundColor;
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 1, KScreenWidth, 104*m6Scale)];
        cancelButton.backgroundColor = [UIColor whiteColor];
        [cancelButton setTitle:NSLocalString(@"取消分享") forState:0];
        [cancelButton setTitleColor:[UIColor colorWithHexString:@"666666"] forState:0];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:28*m6Scale];
        [cancelButton addTarget:self action:@selector(clickCancleBtn) forControlEvents:UIControlEventTouchUpInside];
        
        [footerview addSubview:cancelButton];
        reusableView = footerview;
    }
    return reusableView;
}


#pragma mark- collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if ([self.delegate respondsToSelector:@selector(selectPublicServerItem:)]) {
        [self.delegate selectPublicServerItem:indexPath.row];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(KScreenWidth / 4, 200 * m6Scale);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}

#pragma mark- UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 过滤掉UIButton，也可以是其他类型
    if ( [touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

- (void)clickCancleBtn {

    if ([self.delegate respondsToSelector:@selector(SharePublicServerViewClickCancelBtn)]) {
        [self.delegate SharePublicServerViewClickCancelBtn];
    }
}

- (void)hiddenView {
    if ([self.delegate respondsToSelector:@selector(SharePublicServerViewClickCancelBtn)]) {
        [self.delegate SharePublicServerViewClickCancelBtn];
    }
}



@end
